import axios from "axios";
import { Loading,Message } from "element-ui";
const myaxios = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 5000
})
const loading={
    loadingInstance:null,
    open:function(){
        if(this.loadingInstance===null){
            this.loadingInstance=Loading.service({target:'.main',text:'玩命加载中...',background:'rgba(0,0,0,0.5)'})
        }        
    },
    close:function(){
        if(this.loadingInstance!==null){
            this.loadingInstance.close()
        }
        this.loadingInstance=null
    }   
}
/* myaxios.get('data.json')  //自动会到public目录下找data.json
.then(resp=>{
    console.log(resp.data)
})
.catch(err=>{
    console.log("请求失败")
}) */
// 请求拦截器
myaxios.interceptors.request.use(function (config) {
    // Do something before request is sent
    loading.open()
    return config;
}, function (error) {
    loading.close()
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截器
myaxios.interceptors.response.use(function (response) {
    loading.close()
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    const resp=response.data
    if(resp.code!=200){
        Message({
            message:resp.message||'系统异常',
            type:'warning',
            duration:3000,
            showClose:true
        })
    }
    return response;
}, function (error) {
    loading.close()
    Message({
        message:error.message,
        type:'error',
        duration:3000,
        showClose:true
    })
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
});

export default myaxios